<?php
/**
 * regd by PhpStorm.
 * User: lee
 * Date: 2017/11/29
 * Time: 17:31
 */

namespace Biz\validate;

use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\ShippingTempl as mShippingTempl;
use Biz\service\Category as sCategory;
use Doraemon\model\Tag as mTag;

class GoodsExtendInfo extends \Doraemon\pockets\validate\Base
{

    public static function rules()
    {

        $shippingTimeTypeList = array_keys(mGoodsExtendInfo::$shippingTimeTypeDesc);

        return [

            ['shipping_templ_id', 'required', null, '请选择配送模板', ['create', 'update']],
            ['shipping_templ_id', 'callback', ['callback' => [new self(), 'checkShippingTemplId']], '请正确选择配送模板', ['create', 'update']],

            ['shipping_time_type', 'required', null, '请选择发货时间分类', ['create', 'update', 'saveshippingtime']],
            ['shipping_time_type', 'InList', ['list' => $shippingTimeTypeList], '请正确选择时间分类', ['create', 'update', 'saveshippingtime']],

            ['shipping_time', 'required', null, '请填写或选择发货时间', ['create', 'update', 'saveshippingtime']],
            ['shipping_time', 'callback', ['callback' => [new self(), 'checkShppingTime']], '发货时间超过6天，请选择「非现货」', ['create', 'update', 'saveshippingtime']],
            ['shipping_time', 'callback', ['callback' => [new self(), 'checkShppingTime']], '非现货商品发货时间最少为7天', ['create', 'update', 'saveshippingtime']],


            ['images_content', 'required', null, '请上传图片', ['create', 'update']],
            ['images_content', 'callback', ['callback' => [new self(), 'checkImagesContent']], '商品图片最多支持8张图', ['create', 'update']],

            ['style_content', 'callback', ['callback' => [new self(), 'checkStyleContent']], '请正确填写风格', ['create', 'update']],

            ['material_content', 'callback', ['callback' => [new self(), 'checkMaterialContent']], '请正确填写材质', ['create', 'update']],


            ['content', 'required', null, '请填写商品详情信息', ['create', 'update']],
            ['content', 'length', '2,8000', '商品详情信息太多，请缩短', ['create', 'update']],
            ['content', 'callback', ['callback' => [new self(), 'checkContent']], '商品详情内容不可含有视频内容', ['create', 'update']],

            ['inspection_report', 'callback', ['callback' => [new self(), 'checkInspectionReport']], '质检报告最多支持10张图', ['create', 'update']],

            ['spec_video', 'callback', ['callback' => [new self(), 'checkSpecVideo']], '请正确上传视频', ['create', 'update']],
            ['spec_cover', 'callback', ['callback' => [new self(), 'checkSpecCover']], '请正确上传视频封面', ['create', 'update']],
            ['spec_cover', 'callback', ['callback' => [new self(), 'checkSpecVideoAndCover']], '请同时上传视频和视频封面', ['create', 'update']],

        ];
    }

    public function checkShippingTemplId($value, $valueIdentifier, $allData)
    {
        $templ = mShippingTempl::getTemplById($allData['uid'], $value, mShippingTempl::STATUS_PASSED);

        return !empty($templ);
    }

    public function checkShppingTime($value, $valueIdentifier, $allData)
    {

        if ($allData['shipping_time_type'] == mGoodsExtendInfo::SHIPPING_TIME_TYPE_SPOT) {

            return in_array($value, mGoodsExtendInfo::$shippingTimeList[mGoodsExtendInfo::SHIPPING_TIME_TYPE_SPOT]);
        } elseif ($allData['shipping_time_type'] == mGoodsExtendInfo::SHIPPING_TIME_TYPE_PRE_SALE) {

            if ($allData['shipping_time'] < 7 || !preg_match('/^\d+$/', $allData['shipping_time'])) {
                return false;
            }
            return true;
        }
    }

    public function checkImagesContent($value, $valueIdentifier, $allData)
    {
        if (empty($value)) {
            return false;
        }

        $value = json_decode($value, true);
        foreach ($value as $val) {
            if (!preg_match('/^[a-zA-Z0-9]+$/', $val)) {
                return false;
            }
        }

        //商品图片最多支持8张图
        if (count($value) > 8) return false;

        return true;
    }

    public function checkStyleContent($value, $valueIdentifier, $allData)
    {
        if (empty($value)) {
            return true;
        }

        if (!preg_match('/^\d[0-9,]*\d$/', $value)) {
            return false;
        }

        $ids = explode(',', $value);

        //最多填写3个风格标签
        if (count($ids) > 3) return false;

        $ret = mTag::getTagsByIds($ids, 'tagtype');

        $ret = array_filter($ret, function ($val) {

            return $val == mTag::PARENT_TYPE_FOR_STYLE;
        });

        if (count($ids) !== count($ret)) return false;

        return true;

    }


    public function checkMaterialContent($value, $valueIdentifier, $allData)
    {
        if (empty($value)) {
            return true;
        }
        $data = sCategory::mgtGetCategoryInfoTempl($allData['second_category_id']);

        if (empty($data['material'])) {
            return true;
        }

        if ($value) {
            $list = array_column($value, 'title');

            foreach ($list as $val) {
                if (!in_array($val, $data['material']))
                    return false;
            }
        }

        return true;

    }

    public function checkContent($value, $valueIdentifier, $allData)
    {
        //判断是否含有script脚本
        //判断是否含有object对象
        //判断是否含有video标签
        preg_match('/(\<script|\<object|\<video)?/', $value, $matches);

        if (isset($matches[1]) && !is_null($matches[1])) {
            return false;
        }
        return true;
    }

    public function checkInspectionReport($value, $valueIdentifier, $allData)
    {

        if (empty($value)) return true;

        $value = json_decode($value, true);

        foreach ($value as $val) {
            if (!preg_match('/^[a-zA-Z0-9]+$/', $val)) {
                return false;
            }
        }

        //质检报告图片最多支持10张图
        if (count($value) > 10) return false;

        return true;
    }

    public static function checkSpecVideo($value)
    {
        if (empty($value)) return true;

        if (!preg_match('/^[a-zA-Z0-9]+$/', $value) || strlen($value) != 32) {
            return false;
        }

        return true;
    }

    public static function checkSpecCover($value)
    {
        if (empty($value)) return true;

        if (!preg_match('/^[a-zA-Z0-9]+$/', $value)) {
            return false;
        }

        return true;
    }

    public static function checkSpecVideoAndCover($value, $valueIdentifier, $allData)
    {
        if (!empty($value) && !empty($allData['spec_video'])) {
            return true;
        } else if (empty($value) && empty($allData['spec_video'])) {
            return true;
        } else {
            return false;
        }
    }

}